package userModel

import (
	//	"fmt"
	"errors"
	"git.oschina.net/zd04/beego-shop/models/logModel"
	"git.oschina.net/zd04/beego-shop/utils"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

//用户表
type Users struct {
	Id       int64  `orm:"pk;column(user_id);"`
	Username string `orm:"column(user_name);"`
	Password string `orm:"column(user_password);"`
	Ip       string `orm:"column(last_ip);"`
}

func (this *Users) TableName() string {
	return utils.TableName("users")
}

func init() {
	orm.RegisterModel(new(Users))
}

func LoginUser(username, password string) (user Users, err error) {
	o := orm.NewOrm()
	// qs := o.QueryTable(utils.TableName("users"))
	qs := o.QueryTable(new(Users))
	cond := orm.NewCondition()

	cond = cond.And("user_name", username)
	pwdmd5 := utils.Password(password)
	//	cond = cond.And("user_password", pwdmd5)

	qs = qs.SetCond(cond)
	var users Users
	err = qs.Limit(1).One(&users)

	if err != nil {
		users = Users{}
		err = errors.New("账号不存在!")
	} else if users.Password != pwdmd5 {
		users = Users{}
		err = errors.New("账号或密码错误!")
	}

	return users, err
}

func Management() (userlist []Users, err error) {

	o := orm.NewOrm()
	qs := o.QueryTable(utils.TableName("users"))
	cond := orm.NewCondition()
	qs = qs.SetCond(cond)

	// var userlists []Users
	_, err = qs.All(&userlist)

	logModel.AddLogInfo("test", "test", "test", "test")

	return userlist, err
}

//获取数据的
func GetUser(userid int64) (user Users, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(utils.TableName("users"))
	cond := orm.NewCondition()

	cond = cond.And("user_id", userid)

	qs = qs.SetCond(cond)
	err = qs.Limit(1).One(&user)

	return user, err
}

func GetUserByName(name string) (user Users, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(utils.TableName("users"))
	cond := orm.NewCondition()

	cond = cond.And("user_name", name)

	qs = qs.SetCond(cond)
	err = qs.Limit(1).One(&user)

	beego.Debug(user)
	return user, err
}

//使用字典的方式获取结果，不需要预先写字段信息的
func GetUser2(userid int64) (user orm.Params, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(utils.TableName("users"))
	cond := orm.NewCondition()

	cond = cond.And("user_id", userid)

	qs = qs.SetCond(cond)
	users := []orm.Params{}
	count, err := qs.Limit(1).Values(&users)

	if count > 0 {
		user = users[0]
	}
	beego.Debug(user)
	return user, err
}

func GetUser3(userid int64) (user orm.Params, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(utils.TableName("users"))
	cond := orm.NewCondition()

	cond = cond.And("user_id", userid)

	qs = qs.SetCond(cond)
	users := []orm.Params{}
	count, err := qs.Limit(1).Values(&users)

	if count > 0 {
		user = users[0]
	}
	beego.Debug(user)
	return user, err
}

//添加数据的,添加单个的数据
func AddUser(username, password string) (user Users, err error) {
	o := orm.NewOrm()
	o.Using("default")
	user = Users{}

	pwdmd5 := utils.Password(password)
	user.Username = username
	user.Password = pwdmd5
	_, err = o.Insert(&user)

	return user, err
}

//更新数据的，更新单个的数据
func UpdateUser(userid int64, username, password string) (user Users, err error) {
	o := orm.NewOrm()
	o.Using("default")
	user = Users{}
	user.Id = userid

	pwdmd5 := utils.Password(password)
	user.Username = username
	user.Password = pwdmd5
	_, err = o.Update(&user)

	return user, err
}

func UpdateUser2(userid int64, username, password string) (row int64, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(utils.TableName("users"))

	pwdmd5 := utils.Password(password)
	user := make(orm.Params)
	user["Username"] = username
	user["Password"] = pwdmd5

	row, err = qs.Filter("user_id", userid).Update(user)

	return row, err
}

func UpdateUser3(userid int64, username, password string) (row int64, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable((*Users)(nil))

	cond := orm.NewCondition()
	cond = cond.And("user_id", userid)
	qs = qs.SetCond(cond)

	pwdmd5 := utils.Password(password)
	user := make(orm.Params)
	user["Username"] = username
	user["Password"] = pwdmd5

	row, err = qs.Update(user)

	return row, err
}
